package xyz.xtt.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.xtt.admin.constants.ErrorMessage;
import xyz.xtt.admin.entity.TblAuditLog;
import xyz.xtt.admin.entity.TblUser;
import xyz.xtt.admin.exception.ChainException;
import xyz.xtt.admin.mapper.AuditLogMapper;
import xyz.xtt.admin.request.AuditLogRequest;
import xyz.xtt.admin.service.IAuditLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.xtt.admin.service.IUserService;
import xyz.xtt.admin.utils.ToolsUtil;
import xyz.xtt.admin.vo.AuditLogVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-28
 */
@Slf4j
@Service
public class AuditLogServiceImpl extends ServiceImpl<AuditLogMapper, TblAuditLog> implements IAuditLogService {

    @Autowired
    private IUserService userService;
    @Autowired
    private AuditLogMapper auditLogMapper;

    @Override
    public Page<AuditLogVo> getList(AuditLogRequest auditLogRequest) throws Exception {
        MPJLambdaWrapper<TblAuditLog> query = JoinWrappers.lambda(TblAuditLog.class);

        // 判断手机号
        if(StringUtils.isNotBlank(auditLogRequest.getPhone())) {
            String phone = auditLogRequest.getPhone();
            TblUser user = userService.getUserByNameAndPhone("", phone);
            if (user == null) {
               return null;
            }
            query.eq(TblAuditLog::getAppUserId, user.getAppUserId());
        }

        // 类型
        if(auditLogRequest.getResult() != null) {
            query.eq(TblAuditLog::getResult, auditLogRequest.getResult());
        }

        // 分类
        if(auditLogRequest.getAuditType() != null) {
            query.eq(TblAuditLog::getAuditType, auditLogRequest.getAuditType());
        }

        // 开始时间
        if(StringUtils.isNotBlank(auditLogRequest.getStartTime())) {
            try {
                Long start = ToolsUtil.parseDate(auditLogRequest.getStartTime());
                query.ge(TblAuditLog::getCrtTime, start);
            } catch (Exception e) {
                throw new ChainException(ErrorMessage.ARGUMENT_NOT_VALID);
            }
        }

        //结束时间
        if(StringUtils.isNotBlank(auditLogRequest.getEndTime())) {
            try {
                Long end = ToolsUtil.parseDate(auditLogRequest.getEndTime());
                query.le(TblAuditLog::getCrtTime, end);
            } catch (Exception e) {
                throw new ChainException(ErrorMessage.ARGUMENT_NOT_VALID);
            }
        }

        query.leftJoin(TblUser.class, TblUser::getAppUserId, TblAuditLog::getAppUserId)
                .selectAll(TblAuditLog.class)
                .selectAs(TblUser::getNickname, AuditLogVo::getAppUserName)
                .orderByDesc(TblAuditLog::getCrtTime);

        return auditLogMapper.selectJoinPage(
                new Page<>(auditLogRequest.getCurrent(), auditLogRequest.getPage()),
                AuditLogVo.class,
                query);
    }
}
